<template>
  <div class="article-container">
    <!-- 卡片一 -->
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据筛选表单 -->
      <el-form
        v-loading="loading"
        ref="form"
        :model="form"
        label-width="40px"
        size="mini"
      >
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="channelsId" placeholder="请选择">
            <el-option label="全部" :value="null"></el-option>
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, index) in channels"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangeDate"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="loading"
            @click="loadArticleList(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 卡片二 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">查询到{{ total_count }}条数据</div>
      <!-- 数据展示表格 -->
      <el-table
        :data="articles"
        style="width: 100%"
        border
        stripe
        class="list-table"
        size="mini"
        v-loading="loading"
      >
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.cover.images[0]"
              fit="cover"
              lazy
            >
              <div slot="placeholder" class="image-slot">
                加载中
                <span class="dot">...</span>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag :type="articleStatus[scope.row.status].type">
              {{ articleStatus[scope.row.status].text }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"></el-table-column>
        <el-table-column label="操作">
          <!-- 自定义列模板 -->
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="onDeleteArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total_count"
        background
        class="page-list"
        @current-change="onCurrentChange"
        :page-size="pageSize"
        :disabled="loading"
        :current-page.sync="page"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getArticleList,
  getArticleChannels,
  deleteArticle
} from "@/api/article";
export default {
  name: "ArticleIndex",
  data() {
    return {
      form: {
        region: "",
        data1: "",
        resource: ""
      },
      articles: [], //文章数据列表
      articleStatus: [
        { status: 0, text: "草稿", type: "" },
        { status: 1, text: "待审核", type: "info" },
        { status: 2, text: "审核成功", type: "success" },
        { status: 3, text: "审核失败", type: "warning" },
        { status: 4, text: "已删除", type: "danger" }
      ], //状态数据
      total_count: 0, //总数据条数
      pageSize: 10, //每页大小
      status: null, //查询文章的状态，不传就是全部
      channels: [], //文章频道列表
      channelsId: null, //查询文章的频道
      rangeDate: null, //筛选的范围日期
      loading: true, //表单数据加载中
      page: 1 //当前页码
    };
  },
  created() {
    this.loadArticleList(1);
    this.loadArticleChannels();
  },
  methods: {
    //加载文章列表
    loadArticleList(page = 1) {
      // 展示加载中loading
      this.loading = true;
      getArticleList({
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelsId,
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null,
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null //日期筛选判断以免清空后查询报错
      })
        .then(res => {
          const { results, total_count } = res.data.data;
          this.articles = results;
          this.total_count = total_count;

          //关闭加载中 loading
          this.loading = false;
        })
        .catch(err => {});
    },
    // 加载文章频道
    loadArticleChannels() {
      getArticleChannels()
        .then(res => {
          this.channels = res.data.data.channels;
        })
        .catch(err => {});
    },
    // 分页
    onCurrentChange(page) {
      this.loadArticleList(page);
    },
    // 删除
    onDeleteArticle(articleId) {
      this.$confirm("确定删除吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //确认执行就以下
          deleteArticle(articleId.toString()).then(res => {
            // 删除成功，更新文章数据列表
            this.loadArticleList(this.page);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="less" scoped>
.filter-card {
  margin-bottom: 30px;
}
.list-table {
  margin-bottom: 20px;
}
.page-list {
  float: right;
}
.article-cover {
  width: 100px;
  background-size: cover;
}
</style>
